package com.zzzyh.blogbyspring.controller;

import com.zzzyh.blogbyspring.pojo.UserInfo;
import com.zzzyh.blogbyspring.service.BlogService;
import com.zzzyh.blogbyspring.service.UserService;
import com.zzzyh.blogbyspring.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class User {

    @Autowired
    private UserService userService;

    @Autowired
    private BlogService blogService;
    @RequestMapping("/getUserInfo")
    public UserInfo getUserInfo(HttpServletRequest request) {
        String token = request.getHeader("user_token");
        Claims claims = JwtUtils.postToken(token);
        UserInfo user = userService.getUserInfoById((Integer) claims.get("id"));
        return user;
    }

    @RequestMapping("/getAuthorInfo")
    public UserInfo getAuthorInfo(Integer id) {
        Integer uid = blogService.getUidByBlogId(id);
        return userService.getUserInfoById(uid);
    }
}
